package org.qingke.mail.item.service.provider;

import java.util.List;

import org.qingke.mail.cache.CacheContext;
import org.qingke.mail.common.ServerResponse;
import org.qingke.mail.domain.TbItemCat;
import org.qingke.mail.mapper.TbItemCatMapper;
import org.qingke.mail.util.JsonUtils;
import org.qingke.mail.util.LoggerUtil;
import org.qingke.mail.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 商品分类实现类
 * @author Administrator
 *
 */
@Service("itemCatService")
public class ItemCatServiceImpl extends CacheContext implements ItemCatService {
   
	@Autowired
	private TbItemCatMapper  ibItemCatMapper;
	
	//缓存业务键
	private String ITEMCAT_CACHE_KEY="ITEMCAT_CACHE_KEY";
	
	//日志对象
	private LoggerUtil logger=LoggerUtil.getLogger(getClass());
	
	@Override
	public ServerResponse queryByParentId(long parentId) {
		//构建响应对象
		ServerResponse response=ServerResponse.getInstance();
		//查询缓存
		String dataKey="queryByParentId"+parentId;
		Object cacheData=hGetCache(ITEMCAT_CACHE_KEY, dataKey);
		if(ObjectUtil.notEmpty(cacheData)){
			response.setData(JsonUtils.jsonToList(cacheData.toString(), TbItemCat.class));
			response.setState(true);
			logger.info("get cache"+dataKey);
			return response;
		}
		
		//查询数据
		List<TbItemCat> itemCats=ibItemCatMapper.queryByParentId(parentId);
		
		boolean state=null!=itemCats&&itemCats.size()>0;
		if(state){
			
			//缓存数据
			cacheData=JsonUtils.objectToJson(itemCats);
			hSetCache(ITEMCAT_CACHE_KEY, dataKey, cacheData);
			response.setData(itemCats);
			response.setState(true);
			logger.info("set cache " + dataKey);
		}else{
			response.setState(false);
			response.setMessage("not data");
		}
		return response;
	}
   
}
